<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Koa Test</title>
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  <style>
    body {
      background-color: black;
      color: #fff;
    }
  </style>
</head>
<body>
  <h2>Hello Koa~</h2>

  <p>
    <button id="btn">发起请求</button>
  </p>
  <script>
    document.getElementById('btn').onclick = () => {
      axios.post('/api', {
        apiName: 'abc',
        someParams: {
          aa: '11',
          bb: 22
        }
      })
        .then(function (response) {
          console.log(response);
        })
        .catch(function (error) {
          console.log(error);
        });
    }
  </script>


  <p>
    <form action="/upload" method="post" enctype="multipart/form-data" target="nm_iframe">
      <input type="file" name="file" id="file" value="" multiple="multiple" />
      <input type="submit" value="提交"/>
    </form>
    <iframe id="id_iframe" name="nm_iframe" style="display:none;"></iframe> 
  </p>


  <p>
    <button id="event-source-btn">发起 EventSource</button>
    <p id="content"></p>
  </p>
  <script>
    const eventSourceBtn = document.getElementById("event-source-btn");
    const content = document.getElementById("content");
    let source;
    
    eventSourceBtn.addEventListener('click', () => {
      if (source && source.readyState === source.OPEN) {
        return console.log('source', source);
      }
      source = new EventSource('/event-source');

      let string = '';
      content.innerHTML = '';

      source.onmessage = (e) => {
        if (e.data === 'end') {
          return source.close();
        }
        content.append(e.data);
      }
    });
  </script>


  <p>
    <button id="web-socket-btn">发起 WebSocket</button>
    <p id="content2"></p>
  </p>

</body>
</html>
